﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Text;
using System.Net;

namespace GtsServer.Service.IISWcfService
{
    public partial class WebTest : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            #region

            //string sMessage = "{\"charge\":{\"money\":100,\"appid\":101,\"chargeid\":1,\"mode\":1},\"info\":{\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"position\":2,\"pid\":9999,\"mac\":\"18:87:96:B3:B1:43\",\"height\":800,\"width\":480,\"name\":\"HTC Desire HD A9191\",\"version\":\"2.3.5\"},\"version\":{\"code\":310}}}";

            //string sMessage = "{\"szf\":{\"card\":\"100100100100100100100100\",\"pwd\":\"101101101101101101101\",\"price\":50,\"op\":1},\"charge\":{\"money\":100,\"appid\":101,\"chargeid\":1,\"mode\":2},\"info\":{\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"position\":2,\"pid\":9999,\"mac\":\"18:87:96:B3:B1:43\",\"height\":800,\"width\":480,\"name\":\"HTC Desire HD A9191\",\"version\":\"2.3.5\"},\"version\":{\"code\":310}}}";

            //string sMessage = "{\"info\":{\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"position\":2,\"height\":0,\"width\":0,\"name\":\"HTC Desire HD A9191\",\"appid\":100,\"pid\":100,\"mac\":\"18:87:96:B3:B1:43\",\"version\":\"2.3.5\"},\"version\":{\"code\":310}}}";

            //string sMessage = "{\"szf\":{\"op\":0,\"card\":\"12766080165907289\",\"pwd\":\"081412963388656745\",\"price\":\"30\"},\"charge\":{\"money\":100,\"param\":\"1\",\"chargeid\":0,\"mode\":2},\"info\":{\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"appid\":100,\"pid\":100,\"mac\":\"18:87:96:B3:B1:43\",\"height\":800,\"width\":480,\"name\":\"HTC Desire HD A9191\",\"version\":\"2.3.5\"},\"version\":{\"code\":310}}}";

            //string sMessage = "{\"info\":{\"phone\":{\"height\":480,\"mac\":\"18:87:96:B3:B1:43\",\"name\":\"super apple\",\"pid\":1,\"position\":\"c:\\fm2.apk\",\"version\":\"2.3.4\",\"width\":320},\"sim\":{\"area\":\"13800210500\",\"imei\":\"123456789012345\",\"imsi\":\"123456789012345\",\"number\":\"15201877668\",\"plmn\":\"46000\"},\"version\":{\"code\":290}}}";

            string sMessage = "{\"info\":{\"user\":{\"uid\":\"460022018077374\",\"sys_account\":\"00000375\"},\"sim\":{\"plmn\":\"46000\",\"number\":\"15201877668\",\"imsi\":\"460022018077374\",\"imei\":\"358920045683155\"},\"phone\":{\"appid\":100,\"pid\":100,\"mac\":\"18:87:96:B3:B1:43\",\"height\":800,\"width\":480,\"name\":\"HTC Desire HD A9191\",\"version\":\"2.3.5\"},\"version\":{\"code\":326}}}";

            //string DsmpUrl = context.Request.Url.ToString().Replace("test.ashx", "") + "TaskAPI.ashx";

            string DsmpUrl = Request.Url.ToString().Replace("WebTest.aspx", "") + "TestService.svc/GetClaimGoodsList";

            //string DsmpUrl = "http://localhost:2001/API.ashx";

            //把sMessage发送到指定的DsmpUrl地址上     
            Encoding encode = System.Text.Encoding.GetEncoding("gbk");
            byte[] arrB = encode.GetBytes(sMessage);
            HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(DsmpUrl);
            myReq.Method = "POST";
            myReq.ContentType = "text/xml";
            myReq.ContentLength = arrB.Length;
            Stream outStream = myReq.GetRequestStream();
            outStream.Write(arrB, 0, arrB.Length);
            outStream.Close();


            //接收HTTP做出的响应
            WebResponse myResp = myReq.GetResponse();
            Stream ReceiveStream = myResp.GetResponseStream();
            StreamReader readStream = new StreamReader(ReceiveStream, encode);
            Char[] read = new Char[256];
            int count = readStream.Read(read, 0, 256);
            string str = null;
            while (count > 0)
            {
                str += new String(read, 0, count);
                count = readStream.Read(read, 0, 256);
            }
            readStream.Close();
            myResp.Close();

            Response.Write(str);

            #endregion
        }
    }
}